package com.shux.common.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.shux.common.dao.IBaseBeanDao;
/**
 * 
 * @author Simba.Hua
 *
 * @param <T>
 */
@Repository("baseBeanDao")
public abstract class BaseBeanDaoImpl<T> implements IBaseBeanDao<T>{
    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;
    
    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @SuppressWarnings("hiding")
    @Override
    @Transactional
    public <T> int saveEntity(String sqlId, T obj) throws Exception {
        return sqlSessionTemplate.insert(getWorkSpaceName()+"."+sqlId, obj);
    }

    @Override
    @Transactional
    public <T> int update(String sqlId, T obj) throws Exception {
        if(null == obj){
            return sqlSessionTemplate.update(getWorkSpaceName()+"."+sqlId);
        }else{
            return sqlSessionTemplate.update(getWorkSpaceName()+"."+sqlId, obj);
        }
         
    }

    @Override
    @Transactional
    public int delete(String sqlId, T obj) throws Exception {
        // TODO Auto-generated method stub
        return sqlSessionTemplate.delete(getWorkSpaceName()+"."+sqlId, obj);
    }

    @Override
    public T findForObject(String sqlId, Object obj) throws Exception {
        if(null == obj){
            return sqlSessionTemplate.selectOne(getWorkSpaceName()+"."+sqlId);
        }else{
            return sqlSessionTemplate.selectOne(getWorkSpaceName()+"."+sqlId, obj);
        }
    }

    @Override
    public List<T> findForList(String sqlId, Object obj) throws Exception {
        // TODO Auto-generated method stub
        return sqlSessionTemplate.selectList(getWorkSpaceName()+"."+sqlId, obj);
    }
    


}
